﻿#pragma once

#include <qobject.h>
#include <QHash>

class ObjAutoInject
{
public:
  explicit ObjAutoInject();
  virtual ~ObjAutoInject();
protected:
  static void printInjection(QObject* const obj1,QObject* const obj2);
public:
  virtual void objectAutoInjection(QHash<QString,QObject*>* objMap);
public:
  template<class A> static QString getClassName()
#if defined WIN32 || defined WIN64
  {return (char*)&((typeid (A).name()[6]));}
#endif
#if defined (linux)
  {return handleClassNameInLinux(typeid (A).name());}
  static QString handleClassNameInLinux(const char* begin)
  {return ('0'<=begin[0]&&'9'>=begin[0])?handleClassNameInLinux(begin+1):begin;}
#endif
  template<class A> static QString getClassNameFirstLow()
  {return toFirstLow(getClassName<A>());}
  static QString toFirstLow(QString str)
  {return str.replace(0,1,str.at(0).toLower());}
  template<class A>static A* getObjFromMap(QHash<QString,QObject*>* objMap)
  {return getObjFromMap<A>(objMap,getClassNameFirstLow<A>());}
  template<class A>static A* getObjFromMap(QHash<QString,QObject*>* objMap,QString objName)
  {return objMap->contains(objName)? dynamic_cast<A*>(objMap->value(objName)):Q_NULLPTR;}
  template<class A>bool isMapContains(QHash<QString,QObject*>* objMap)
  {return objMap->contains(getClassNameFirstLow<A>());}
};
